/**
 * @FilePath     : \simulations\include\agvNavigate\opInclude\operation.h
 * @Description  : 定义了操作类Operation及其相关方法和状态，主要用于AGV导航中的动作管理和状态控制。 
 *                 包含操作的初始化、执行、结束及状态检查等功能，支持地标二维码的验证逻辑。  
 * @Author       : xiujun.yang
 * @Version      : 1.0.0
 * @LastEditors  : error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime : 2024-12-19 16:30:07
 * @Copyright (c) 2024 by 临工智能信息科技有限公司, All Rights Reserved. 
**/
#pragma once
#include "z_types.h"
#include <memory>
#include "rclcpp_action/rclcpp_action.hpp"
#include <agv_msgs/msg/action.hpp>
#include <agv_msgs/msg/action_param.hpp>
#include <mutex>
#include <thread>
#include "tools.h"
#include "public_macro.h"
// #include "public_kernel.h"
//////////////////////////////////////////////////////////////////////////////
// #1  blockingtype:
//   Enum {NOTE, SOFT, HARD}:
//   "NONE"- allows driving and other actions;
//   "SOFT"- allows other actions, but not driving;
//   "HARD"- is the only allowed action at that time.
constexpr const char* DYN_TYPE = "NONE";
constexpr const char* OP_MULTI_TYPE = "SOFT";
constexpr const char* OP_MUTEX_TYPE = "HARD";

// string Enum {WAITING; INITIALIZING; RUNNING; PAUSED; FINISHED; FAILED}
constexpr const char* STATUS_WAIT = "WAITING";
constexpr const char* STATUS_INIT = "INITIALIZING";
constexpr const char* STATUS_RUN = "RUNNING";
constexpr const char* STATUS_PAUSE = "PAUSED";
constexpr const char* STATUS_FINISH = "FINISHED";
constexpr const char* STATUS_FAILED = "FAILED";

constexpr const char* BOOL_TYPE = "BOOL";
constexpr const char* NUMBER_TYPE = "NUMBER";
constexpr const char* INT_TYPE = "INTEGER";
constexpr const char* FLOAT_TYPE = "FLOAT";
constexpr const char* STR_TYPE = "STRING";
constexpr const char* OBJ_TYPE = "OBJECT";
constexpr const char* ARRAY_TYPE = "ARRAY";

constexpr const char* NAME_KEY_ACTION_QR_ID = "nodeQrId";          // 动作参数执行时，节点二维码id校验 KEY 名字
constexpr const char* NAME_KEY_ACTION_ENABLE_QR_ID = "enableQrId"; // 动作参数执行时，是否开启节点二维码id校验 KEY 名字
//////////////////////////////////////////////////////////////////////////////
//   The interface of class "Operation".
class Operation
{
 public:
    explicit Operation(const agv_msgs::msg::Action& action);
    // The destructor
    virtual ~Operation() = default;
    // Get the current working stage
    virtual SHORT GetCurStage();
    // The routine to be called in each working cycle
    virtual void CycleRoutine();
    // Force to stop the operation
    virtual bool ForceStop();
    // Check whether the operation has finished
    virtual bool Finished();
    // Initialize the related data
    virtual bool Init();
    // Ready to start new op
    virtual bool ReadyStart();
    // Defines how to start the operation
    virtual bool StartAction();
    // Defines how to end the operation
    virtual bool EndAction();
    // Defines how to do the operation
    virtual bool Work();
    // Get dynamic operation at end node
    int GetDynOpEndNode();
    // Get the current action object
    virtual agv_msgs::msg::Action GetCurActionObj();
    // 复位参数
    virtual void ResetOpValue();
    // 检测地标验证逻辑
    bool TryCycleMarkCheck();
    
protected:
    enum OpStage {
        OpInit = 0,
        OpReadyStart = 1,
        OpStart = 2,
        OpWork = 3,
        OpReadyEnd = 4,
        OpEnd = 5,
        OpError = 6
    };

    // 地码校验
    struct MarkCodeVerify {
        int node_qr_id;
        int enable;
        MarkCodeVerify() {
            node_qr_id = -1;
            enable = 0;
        }
    };

    USHORT total_count_;             // Total count for the operation
    SHORT stage_;                    // The current operation stage
    agv_msgs::msg::Action action_;
    int dyn_op_;                     // dynOpEndNode (only for dyn op task)
    bool finished_;
    int delay_count_;                // Delay count for the operation
    int first_delay_count_;          // First count for the operation
    std::mutex mutex_;
    MarkCodeVerify mark_verify_;     // 节点二维码id,校验地标用
    TimerTriggerOnce timer_trigger_; // 地码校验时间验证

 private:
    // 查找qrId参数是否存在
    bool FindQrId();

    // 到点校验地标id是否正确
    bool CheckMarkQrId(std::string &err_msg);

    // 设置地标矫正错误事件
    void SetLocMissEvent(std::string msg, bool occur);

    // 设置错误
    //  virtual  void SetError(int id,string detail,int ctrol_mode,bool occur);

    // for perial call back function
    // void FeedbackCb(const agv_actions::DoPeripheralFeedbackConstPtr &feedback);

    // void DoneCb(const actionlib::SimpleClientGoalState &state, const agv_actions::DoPeripheralResultConstPtr &result);

    // void ActiveCb();
};

